package GUIHelper;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;

import ControlLayer.ActivityTypeCtr;
import GUILayer.LoginUI;
import ModelLayer.ActivityType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JCheckBox;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ActivityPanel extends JPanel {
	private DefaultTableModel model;

	private JTextField txtSearch;
	private JButton btnRefresh;

	private JTable tblActivities;

	private JTextField txt_actID;
	private JTextField txt_actName;
	private JTextField txt_actPriceF;
	private JTextField txt_actPriceI;
	private JTextField txt_maxPart;
	private JTextField txt_minPart;

	private JButton btnNewActivity;
	private JButton btnEdit;
	private JButton btnClear;
	private JButton btnDelete;
	private JButton btnSave;

	private boolean isNew = false;

	private JCheckBox chckbxGroupActivity;

	private int authLevel;

	public ActivityPanel() {
		authLevel = LoginUI.getLoggedInPosition();
		setLayout(new BorderLayout(0, 0));

		JPanel panel_activityList = new JPanel();
		add(panel_activityList, BorderLayout.WEST);
		panel_activityList.setLayout(new BorderLayout(0, 0));

		JPanel panel_activitySearch = new JPanel();
		panel_activityList.add(panel_activitySearch, BorderLayout.NORTH);

		txtSearch = new JTextField();
		txtSearch.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				populateTable();
			}
		});
		txtSearch.setColumns(10);

		btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtSearch.setText("");
				populateTable();
			}
		});
		GroupLayout gl_panel_activitySearch = new GroupLayout(
				panel_activitySearch);
		gl_panel_activitySearch.setHorizontalGroup(gl_panel_activitySearch
				.createParallelGroup(Alignment.LEADING).addGroup(
						gl_panel_activitySearch
								.createSequentialGroup()
								.addContainerGap()
								.addComponent(txtSearch,
										GroupLayout.DEFAULT_SIZE, 355,
										Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnRefresh).addContainerGap()));
		gl_panel_activitySearch
				.setVerticalGroup(gl_panel_activitySearch
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_activitySearch
										.createSequentialGroup()
										.addGap(6)
										.addGroup(
												gl_panel_activitySearch
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																txtSearch,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																btnRefresh))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		panel_activitySearch.setLayout(gl_panel_activitySearch);

		JScrollPane scrollPane = new JScrollPane();
		panel_activityList.add(scrollPane, BorderLayout.CENTER);

		model = new DefaultTableModel(new Object[][] {}, new String[] { "ID",
				"Name" }) {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};

		tblActivities = new JTable(model);
		tblActivities.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				ActivityType a = new ActivityTypeCtr().getActivity((Long) model
						.getValueAt(tblActivities.getSelectedRow(), 0));

				txt_actID.setText(Long.toString(a.getID()));
				txt_actName.setText(a.getName());
				txt_actPriceF.setText(Double.toString(a
						.getPricePerHour_Facility()));
				txt_actPriceI.setText(Double.toString(a
						.getPricePerHour_Instructor()));
				txt_maxPart.setText(Integer.toString(a.getMaxParticipants()));
				txt_minPart.setText(Integer.toString(a.getMinParticipants()));

				chckbxGroupActivity.setSelected(a.isGroupActivity());
				enableSelected();
			}
		});
		tblActivities.getColumnModel().getColumn(0).setPreferredWidth(100);
		tblActivities.getColumnModel().getColumn(0).setMaxWidth(100);
		tblActivities.setFillsViewportHeight(true);
		tblActivities.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane.setViewportView(tblActivities);

		JPanel panel_details = new JPanel();
		add(panel_details, BorderLayout.CENTER);
		panel_details.setLayout(new BorderLayout(0, 0));

		JPanel panel_buttons = new JPanel();
		panel_details.add(panel_buttons, BorderLayout.SOUTH);

		if (authLevel == 1 || authLevel == 2 || authLevel == 3) {
			btnNewActivity = new JButton("New Activity");
			btnNewActivity.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					isNew = true;
					enableEdit();
				}
			});
			panel_buttons.add(btnNewActivity);

			btnEdit = new JButton("Edit");
			btnEdit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					isNew = false;
					enableEdit();
				}
			});
			btnEdit.setEnabled(false);
			panel_buttons.add(btnEdit);

			btnClear = new JButton("Clear");
			btnClear.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					isNew = false;
					enableClear();
				}
			});
			btnClear.setEnabled(false);
			panel_buttons.add(btnClear);

			btnDelete = new JButton("Delete");
			btnDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ActivityTypeCtr ctr = new ActivityTypeCtr();
					ctr.deleteActivity(new ActivityType(Long
							.parseLong(txt_actID.getText())));
					JOptionPane
							.showMessageDialog(null,
									"Activity removed successfully!",
									"Activity Deleted",
									JOptionPane.INFORMATION_MESSAGE);
					enableClear();
					populateTable();
				}
			});
			btnDelete.setEnabled(false);
			panel_buttons.add(btnDelete);

			btnSave = new JButton("Save");
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ActivityTypeCtr ctr = new ActivityTypeCtr();
					ActivityType a = new ActivityType();
					if (!isNew)
						a.setID(Long.parseLong(txt_actID.getText()));
					else
						a.setID(0);
					a.setName(txt_actName.getText());
					a.setGroupActivity(chckbxGroupActivity.isSelected());
					if (chckbxGroupActivity.isSelected()) {
						a.setMinParticipants(Integer.parseInt(txt_minPart
								.getText()));
						a.setMaxParticipants(Integer.parseInt(txt_maxPart
								.getText()));
					}

					a.setPricePerHour_Facility(Double.parseDouble(txt_actPriceF
							.getText()));
					a.setPricePerHour_Instructor(Double
							.parseDouble(txt_actPriceI.getText()));

					if (isNew) {
						ctr.insertActivity(a);
						JOptionPane.showMessageDialog(null,
								"New activity addedd successfully!",
								"Activity added",
								JOptionPane.INFORMATION_MESSAGE);
					} else {
						ctr.updateActivity(a);
						JOptionPane.showMessageDialog(null,
								"Activity updated successfully!",
								"Activity updated",
								JOptionPane.INFORMATION_MESSAGE);
					}

					enableClear();
					txtSearch.setText("");
					populateTable();

				}
			});
			btnSave.setEnabled(false);
			panel_buttons.add(btnSave);
		}

		JPanel panel_data = new JPanel();
		panel_data.setBorder(new TitledBorder(null, "Activity Details",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_details.add(panel_data, BorderLayout.CENTER);

		JPanel panel_generic = new JPanel();
		panel_generic.setBorder(new TitledBorder(null, "Generic Information",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel panel_price = new JPanel();
		panel_price.setBorder(new TitledBorder(null, "Price",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel panel_participants = new JPanel();
		panel_participants.setBorder(new TitledBorder(null, "Participants",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout gl_panel_data = new GroupLayout(panel_data);
		gl_panel_data.setHorizontalGroup(gl_panel_data
				.createParallelGroup(Alignment.LEADING)
				.addComponent(panel_generic, GroupLayout.DEFAULT_SIZE, 385,
						Short.MAX_VALUE)
				.addComponent(panel_price, GroupLayout.DEFAULT_SIZE, 385,
						Short.MAX_VALUE)
				.addComponent(panel_participants, GroupLayout.DEFAULT_SIZE,
						385, Short.MAX_VALUE));
		gl_panel_data.setVerticalGroup(gl_panel_data.createParallelGroup(
				Alignment.LEADING)
				.addGroup(
						gl_panel_data
								.createSequentialGroup()
								.addComponent(panel_generic,
										GroupLayout.PREFERRED_SIZE, 117,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(panel_price,
										GroupLayout.PREFERRED_SIZE, 108,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(panel_participants,
										GroupLayout.DEFAULT_SIZE, 156,
										Short.MAX_VALUE)));

		txt_maxPart = new JTextField();
		txt_maxPart.setEnabled(false);
		txt_maxPart.setColumns(10);

		JLabel lblMaximumAmountOf = new JLabel(
				"Maximum amount of participants:");

		JLabel lblMinimumAmountOf = new JLabel(
				"Minimum amount of participants:");

		txt_minPart = new JTextField();
		txt_minPart.setEnabled(false);
		txt_minPart.setColumns(10);

		chckbxGroupActivity = new JCheckBox("Group activity");
		GroupLayout gl_panel_participants = new GroupLayout(panel_participants);
		gl_panel_participants
				.setHorizontalGroup(gl_panel_participants
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								gl_panel_participants
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_participants
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_participants
																		.createSequentialGroup()
																		.addGroup(
																				gl_panel_participants
																						.createParallelGroup(
																								Alignment.TRAILING,
																								false)
																						.addComponent(
																								lblMinimumAmountOf,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								lblMaximumAmountOf,
																								Alignment.LEADING,
																								GroupLayout.PREFERRED_SIZE,
																								189,
																								GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addGroup(
																				gl_panel_participants
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								txt_minPart,
																								GroupLayout.DEFAULT_SIZE,
																								160,
																								Short.MAX_VALUE)
																						.addComponent(
																								txt_maxPart,
																								GroupLayout.DEFAULT_SIZE,
																								160,
																								Short.MAX_VALUE)))
														.addComponent(
																chckbxGroupActivity))
										.addContainerGap()));

		gl_panel_participants
				.setVerticalGroup(gl_panel_participants
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_participants
										.createSequentialGroup()
										.addComponent(chckbxGroupActivity)
										.addGap(5)
										.addGroup(
												gl_panel_participants
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblMaximumAmountOf)
														.addComponent(
																txt_maxPart,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_panel_participants
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblMinimumAmountOf)
														.addComponent(
																txt_minPart,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(59, Short.MAX_VALUE)));
		panel_participants.setLayout(gl_panel_participants);

		txt_actPriceF = new JTextField();
		txt_actPriceF.setEnabled(false);
		txt_actPriceF.setColumns(10);

		JLabel lblFacilityOnly = new JLabel("Facility only:");

		JLabel lblcurrency = new JLabel("{currency}");

		JLabel lblWithInstructor = new JLabel("With instructor:");

		txt_actPriceI = new JTextField();
		txt_actPriceI.setEnabled(false);
		txt_actPriceI.setColumns(10);

		JLabel label = new JLabel("{currency}");
		GroupLayout gl_panel_price = new GroupLayout(panel_price);
		gl_panel_price.setHorizontalGroup(gl_panel_price.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel_price
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								gl_panel_price
										.createParallelGroup(Alignment.LEADING)
										.addComponent(lblFacilityOnly)
										.addComponent(lblWithInstructor))
						.addGap(27)
						.addGroup(
								gl_panel_price
										.createParallelGroup(Alignment.LEADING)
										.addComponent(txt_actPriceI,
												GroupLayout.DEFAULT_SIZE, 198,
												Short.MAX_VALUE)
										.addComponent(txt_actPriceF,
												GroupLayout.DEFAULT_SIZE, 204,
												Short.MAX_VALUE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(
								gl_panel_price
										.createParallelGroup(Alignment.LEADING)
										.addComponent(lblcurrency)
										.addComponent(label,
												GroupLayout.PREFERRED_SIZE, 52,
												GroupLayout.PREFERRED_SIZE))
						.addContainerGap()));
		gl_panel_price
				.setVerticalGroup(gl_panel_price
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_price
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_price
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																txt_actPriceF,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblFacilityOnly)
														.addComponent(
																lblcurrency))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_panel_price
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblWithInstructor)
														.addComponent(
																txt_actPriceI,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(label))
										.addContainerGap(34, Short.MAX_VALUE)));
		panel_price.setLayout(gl_panel_price);

		txt_actID = new JTextField();
		txt_actID.setEnabled(false);
		txt_actID.setColumns(10);

		JLabel lblId = new JLabel("ID:");

		txt_actName = new JTextField();
		txt_actName.setEnabled(false);
		txt_actName.setColumns(10);

		JLabel lblName = new JLabel("Name:");
		GroupLayout gl_panel_generic = new GroupLayout(panel_generic);
		gl_panel_generic
				.setHorizontalGroup(gl_panel_generic
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								Alignment.TRAILING,
								gl_panel_generic
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_generic
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(lblId)
														.addComponent(
																lblName,
																GroupLayout.PREFERRED_SIZE,
																74,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_panel_generic
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(
																txt_actName,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																295,
																Short.MAX_VALUE)
														.addComponent(
																txt_actID,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																264,
																Short.MAX_VALUE))
										.addContainerGap()));
		gl_panel_generic
				.setVerticalGroup(gl_panel_generic
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_generic
										.createSequentialGroup()
										.addGroup(
												gl_panel_generic
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																txt_actID,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblId))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_panel_generic
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																txt_actName,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblName))
										.addContainerGap(48, Short.MAX_VALUE)));
		panel_generic.setLayout(gl_panel_generic);
		panel_data.setLayout(gl_panel_data);

		populateTable();
	}

	private void populateTable() {
		model.setRowCount(0);
		ActivityTypeCtr ctr = new ActivityTypeCtr();

		ArrayList<ActivityType> activities = new ArrayList<>();
		if (txtSearch.getText().length() > 0) {
			ArrayList<ActivityType> activities1 = ctr.getActivities();
			for (ActivityType a : activities1) {
				if (String.valueOf(a.getID()).toLowerCase()
						.contains(txtSearch.getText().toLowerCase())
						|| a.getName().toLowerCase()
								.contains(txtSearch.getText().toLowerCase()))
					activities.add(a);
			}
		} else {
			activities = ctr.getActivities();
		}

		for (ActivityType a : activities) {
			model.addRow(new Object[] { a.getID(), a.getName() });
		}
	}

	private void enableSelected() {
		txt_actName.setEnabled(false);
		txt_actPriceF.setEnabled(false);
		txt_actPriceI.setEnabled(false);
		txt_maxPart.setEnabled(false);
		txt_minPart.setEnabled(false);

		btnNewActivity.setEnabled(false);
		btnEdit.setEnabled(true);
		btnClear.setEnabled(true);
		btnDelete.setEnabled(false);
		btnSave.setEnabled(false);
	}

	private void enableEdit() {
		txt_actName.setEnabled(true);
		txt_actPriceF.setEnabled(true);
		txt_actPriceI.setEnabled(true);
		txt_maxPart.setEnabled(true);
		txt_minPart.setEnabled(true);

		btnNewActivity.setEnabled(false);
		btnEdit.setEnabled(false);
		btnClear.setEnabled(true);
		if (isNew)
			btnDelete.setEnabled(false);
		else
			btnDelete.setEnabled(true);
		btnSave.setEnabled(true);
	}

	private void enableClear() {
		txt_actID.setText("");
		txt_actName.setText("");
		txt_actPriceF.setText("");
		txt_actPriceI.setText("");
		txt_maxPart.setText("");
		txt_minPart.setText("");
		chckbxGroupActivity.setSelected(false);

		txt_actID.setEnabled(false);
		txt_actName.setEnabled(false);
		txt_actPriceF.setEnabled(false);
		txt_actPriceI.setEnabled(false);
		txt_maxPart.setEnabled(false);
		txt_minPart.setEnabled(false);

		btnNewActivity.setEnabled(true);
		btnEdit.setEnabled(false);
		btnClear.setEnabled(false);
		btnDelete.setEnabled(false);
		btnSave.setEnabled(false);
	}
}
